package cn.wolfcode.trip.app.controller;


import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Collect;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.CollectQueryObject;
import cn.wolfcode.trip.base.service.CollectService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 收藏功能的控制器
 */
@RestController
@RequestMapping("collects")
public class CollectController {

    @Autowired
    private CollectService CollectService;

    @DeleteMapping
    public JSONResult delete(Collect collect){
        User user = UserContext.getCurrentUser();
        JSONResult result = new JSONResult();
        if (user != null) {
            collect.setUser(user);
            CollectService.delete(collect);
        }
        return new JSONResult();
    }
    /**
     * 攻略收藏接口
     */
    @PostMapping("/strategy")
    public JSONResult saveStrategy(Collect collect){
        User user = UserContext.getCurrentUser();
        JSONResult result = new JSONResult();
        if (user != null) {
            collect.setUser(user);
            try {
                CollectService.save(collect);
            } catch (Exception e) {
                e.printStackTrace();
                result.mark(e.getMessage());
            }
        }else {
            result.mark("你需要登录后才能收藏该攻略");
        }
        return result;
    }

    /**
     * 游记收藏接口
     * @param collect
     * @return
     */
    @PostMapping("/travel")
    public JSONResult saveTravel(Collect collect){
        User user = UserContext.getCurrentUser();
        JSONResult result = new JSONResult();
        if (user != null) {
            collect.setUser(user);
            try {
                CollectService.save(collect);
            } catch (Exception e) {
                e.printStackTrace();
                result.mark(e.getMessage());
            }
        }else {
            result.mark("你需要登录后才能收藏该游记");
        }
        return result;
    }

    /**
     * 收藏日报的接口
     * @return
     */
    @PostMapping("/dailyNews")
    public JSONResult saveNews(Collect collect){
        User user = UserContext.getCurrentUser();
        JSONResult result = new JSONResult();
        if(user != null){
            collect.setUser(user);
            try {
                CollectService.save(collect);
            } catch (Exception e) {
                e.printStackTrace();
                result.mark(e.getMessage());
            }
        }else{
            result.mark("你需要登录后才能收藏该日报");
        }
        return result;
    }

    /**
     * 查询该用户是否收藏过该游记或者该攻略
     * @param collect
     * @return
     */
    @GetMapping
    public JSONResult get(Collect collect){
        User user = UserContext.getCurrentUser();
        JSONResult result = new JSONResult();
        if (user != null) {
            collect.setUser(user);
            try {
                CollectService.get(collect);
            } catch (Exception e) {
                result.mark(e.getMessage());
            }
        }
        return result;
    }

    @GetMapping("/{userId}/strategy")
    public PageInfo listStrategy(CollectQueryObject qo){
        //按照发布间倒序
        qo.setOrderBy("sc.createTime desc");
        return CollectService.queryStrategy(qo);
    }

    @GetMapping("/{userId}/travel")
    public PageInfo listTravel(CollectQueryObject qo){
        //按照发布间倒序
        qo.setOrderBy("tc.createTime desc");
        return CollectService.queryTravel(qo);
    }

    @GetMapping("/{userId}/dailyNews")
    public  PageInfo listDailyNews(CollectQueryObject qo){
        //按照发布间倒序
        qo.setOrderBy("dc.createTime desc");
        PageInfo pageInfo = CollectService.queryDailyNews(qo);
        return pageInfo;
    }
}
